package com.canbot.u05.activity.presidemode.newcode.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.AppCompatImageView;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;

import com.canbot.u05.R;
import com.canbot.u05.activity.presidemode.newcode.bean.EDirection;
import com.canbot.u05.activity.presidemode.newcode.interfaces.ICircleClick;

public class UnCircleImageView extends android.support.v7.widget.AppCompatImageView {
    private static final String TAG = "UnCircleImageView";
    private EDirection eDirection;
    private String textContent = "左转";
    private int textPading;
    private ICircleClick iCircleClick;
    private Drawable pressDrawable;
    private Drawable background;
    private boolean openText;

    public void setiCircleClick(ICircleClick iCircleClick) {
        this.iCircleClick = iCircleClick;
    }

    public UnCircleImageView(Context context) {
        this(context, null);
    }

    public UnCircleImageView(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
    }

    private long prevPressTime;
    private GestureDetector gestureDetector;

    @SuppressLint("ClickableViewAccessibility")
    public UnCircleImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttribute(context, attrs);
        initData();
        setOnTouchListener((v, event) -> {
            AppCompatImageView imageViewCompat = (AppCompatImageView) v;
            BitmapDrawable drawable = (BitmapDrawable) imageViewCompat.getBackground();
            Bitmap bitmap = drawable.getBitmap();
            int x = (int) event.getX();
            int y = (int) event.getY();
            int action = event.getAction();
            switch (action) {
                case MotionEvent.ACTION_DOWN:
                case MotionEvent.ACTION_MOVE:
                    if (x >= bitmap.getWidth() || x < 0 || y >= bitmap.getHeight() || y < 0 || bitmap.getPixel((x), (y)) == 0) {
                        return true;//透明区域返回true
                    }
                    setBackgroundDrawable(pressDrawable);
                    long currentTimeMillis = System.currentTimeMillis();
                    if (currentTimeMillis - prevPressTime > 200) {
                        prevPressTime = currentTimeMillis;
                        if (iCircleClick != null) {
                            iCircleClick.onPress(this);
                        }
                    }
                    break;
                case MotionEvent.ACTION_CANCEL:
                case MotionEvent.ACTION_UP:
                    setBackgroundDrawable(background);
                    if (iCircleClick != null) {
                        iCircleClick.onUp();
                    }
                    break;
            }
            return false;
        });
        setOnClickListener(v -> {
            if (iCircleClick != null) {
                iCircleClick.onClick(v);
            }
        });
    }

    private TextPaint textPaint;

    private void initData() {
        textPaint = new TextPaint();
        textPaint.setAntiAlias(true);
        textPaint.setColor(getResources().getColor(R.color.blue));
        textPaint.setTextSize(getResources().getDimension(R.dimen.center_text_size));
        textPading = (int) getResources().getDimension(R.dimen.x12);
    }

    private void initAttribute(Context context, AttributeSet attrs) {
        background = getBackground();
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.UnCircleImageView);
        int textPos = typedArray.getInteger(R.styleable.UnCircleImageView_textPos, 0);
        eDirection = EDirection.getName(textPos);
        pressDrawable = typedArray.getDrawable(R.styleable.UnCircleImageView_pressDrawable);
        openText = typedArray.getBoolean(R.styleable.UnCircleImageView_openText, false);
        typedArray.recycle();
    }

    public EDirection geteDirection() {
        return eDirection;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = getWidth();
        int height = getHeight();
        //canvas.drawCircle(width / 2, height / 2, 2, textPaint);
        float textW = textPaint.measureText(textContent);
        Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();

        // 绘制字体，字体的坐标位置是左下角
        float textH = fontMetrics.descent - fontMetrics.ascent;

        if (openText) {
            if (EDirection.UP == eDirection) {
                textContent = getResources().getString(R.string.gohead);
                canvas.drawText(textContent, width / 2 - textW / 2, height / 2 + textH - textPading, textPaint);

            } else if (EDirection.DOWN == eDirection) {
                textContent = getResources().getString(R.string.goback);
                canvas.drawText(textContent, width / 2 - textW / 2, height / 2, textPaint);

            } else if (EDirection.LEFT == eDirection) {
                textContent = getResources().getString(R.string.turnleft);
                String start = textContent.subSequence(0, 1).toString();
                String end = textContent.subSequence(1, 2).toString();
                canvas.drawText(start, width / 2, height / 2 - textPading / 2, textPaint);
                canvas.drawText(end, width / 2, height / 2 + textH - textPading / 2, textPaint);

            } else if (EDirection.RIGHT == eDirection) {
                textContent = getResources().getString(R.string.turnright);
                String start = textContent.subSequence(0, 1).toString();
                String end = textContent.subSequence(1, 2).toString();
                canvas.drawText(start, width / 2 - textW / 2, height / 2 - textPading / 2, textPaint);
                canvas.drawText(end, width / 2 - textW / 2, height / 2 + textH - textPading / 2, textPaint);
            }
        }
    }

    public void select() {
        if (pressDrawable != null) {
            setBackgroundDrawable(pressDrawable);
        }
    }

    public void cancle() {
        if (background != null) {
            setBackgroundDrawable(background);
        }
    }
}
